/**
 * 执行智能分析的核心函数
 */
async function executeAnalysis(analysisType) {
    const caseId = sessionStorage.getItem('currentCaseId');
    if (!caseId) {
        alert('请先选择案件');
        return;
    }

    // 查找分析模型定义
    let analyzerConfig = null;
    for (const category of ANALYSIS_MODELS) {
        if (category.children) {
            const found = category.children.find(child => child.type === analysisType);
            if (found) {
                analyzerConfig = found;
                break;
            }
        }
    }

    if (!analyzerConfig || !analyzerConfig.analyzer) {
        console.error('未找到分析器:', analysisType);
        return;
    }

    // 显示加载中
    const resultContainer = document.getElementById('analysis-result-container');
    if (resultContainer) {
        resultContainer.innerHTML = '<div style="padding:40px; text-align:center;"><div class="loading-spinner"></div><p>正在分析中...</p></div>';
    }

    try {
        // 实例化分析器
        const AnalyzerClass = window[analyzerConfig.analyzer];
        if (!AnalyzerClass) {
            throw new Error(`分析器类未加载: ${analyzerConfig.analyzer}`);
        }

        const analyzer = new AnalyzerClass(caseId);

        // 获取对应的配置参数
        let params = {};
        if (analysisType.startsWith('fast_pass') || analysisType.startsWith('smurfing') ||
            analysisType.startsWith('off_hour') || analysisType.startsWith('special_amount')) {
            params = analysisConfig.financial;
        } else if (analysisType.startsWith('intimacy')) {
            params = analysisConfig.communication;
        }

        // 执行分析
        const result = await analyzer.analyze(params);

        // 渲染结果
        if (resultContainer) {
            let html = '<div class="analysis-results">';

            // 摘要卡片
            const levelColors = {
                'info': '#909399',
                'warning': '#E6A23C',
                'danger': '#F56C6C'
            };
            const levelColor = levelColors[result.level] || '#909399';

            html += `
        <div style="background:white; padding:20px; margin-bottom:20px; border-radius:8px; border-left:4px solid ${levelColor};">
          <h3 style="margin:0 0 10px; color:#303133;">${result.title}</h3>
          <p style="margin:0; color:#606266;">${result.summary}</p>
          <div style="margin-top:10px; font-size:0.9rem; color:#909399;">
            分析时间: ${new Date(result.timestamp).toLocaleString('zh-CN')} | 
            结果数量: <strong>${result.count}</strong>
          </div>
        </div>
      `;

            // 详细结果
            html += analyzer.renderResult(result.data);
            html += '</div>';

            resultContainer.innerHTML = html;
        }
    } catch (error) {
        console.error('分析执行失败:', error);
        if (resultContainer) {
            resultContainer.innerHTML = `
        <div style="padding:40px; text-align:center; color:#F56C6C;">
          <p>❌ 分析执行失败</p>
          <p style="font-size:0.9rem;">${error.message}</p>
        </div>
      `;
        }
    }
}

// 添加到全局
window.executeAnalysis = executeAnalysis;
